
<template>
  <div class="app-container">
    <div>
      <!-- 表单 -->
      <el-form :inline="true" :model="dataForm" ref="dataFormRef" class="demo-form-inline">
        <el-form-item label="ID" prop="id">
          <el-input v-model="dataForm.id" placeholder="请输入ID"></el-input>
        </el-form-item><el-form-item label="标题" prop="title">
          <el-input v-model="dataForm.title" placeholder="请输入标题"></el-input>
        </el-form-item><el-form-item label="作者" prop="author">
          <el-input v-model="dataForm.author" placeholder="请输入作者"></el-input>
        </el-form-item><el-form-item label="日期" prop="display_time">
          <el-input v-model="dataForm.display_time" placeholder="请输入日期"></el-input>
        </el-form-item><el-form-item label="浏览量" prop="pageviews">
          <el-input v-model="dataForm.pageviews" placeholder="请输入浏览量"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleOpenForm({})">新增</el-button>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="author" label="作者"></el-table-column>
        <el-table-column prop="display_time" label="日期"></el-table-column>
        <el-table-column prop="pageviews" label="浏览量"></el-table-column>
        
      <el-table-column fixed="right" label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleOpenForm(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paging.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="paging.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="paging.total">
      </el-pagination>
    </div>
    <Form ref="formRef" @refresh="resetForm"/>
  </div>
</template>

<script>
import { basicRequest } from '@/api/basics'
import Form from './form.vue'
export default {
  components: { Form },
  data() {
    return {
      list: [], // 表格数据
      paging: {
        total: 0, // 总条数
        page: 1, // 当前页
        limit: 10 // 每页条数
      },
      dataForm: { 
        id: '',
        title: '',
        author: '',
        display_time: '',
        pageviews: '',
      }, // 表单数据
      listLoading: true, // 加载状态
    }
  },
  mounted() {
    // 页面加载时获取数据
    this.getList()
  },
  methods: {
    
      handleOpenForm(row) {
        this.$refs.formRef.init(row)
      },
      
    onSubmit() {
      // 查询按钮点击事件，重置页码并获取数据
      this.paging.page = 1
      this.getList()
    },
    resetForm() {
      // 重置按钮点击事件，重置表单并获取数据
      this.$refs.dataFormRef.resetFields()
      this.onSubmit()
    },
    handleSizeChange(val) {
      // 每页条数改变事件，改变每页条数并获取数据
      this.paging.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      // 当前页改变事件，改变当前页并获取数据
      this.paging.page = val
      this.getList()
    },
    handleDelete(row) {
      // 删除按钮点击事件，弹出确认框，确认后调用删除接口
      this.$confirm('是否确定删除该行?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          basicRequest({
            id: row.id
          }, {}, '/vue-admin-template/table/del', 'DELETE').then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.resetForm()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }, 
    getList() {
      // 获取数据方法，根据当前页和每页条数获取数据
      this.listLoading = true
      basicRequest({
        ...this.paging,
        ...this.dataForm
      }, {}, '/vue-admin-template/table/list', 'GET').then(response => {
        this.list = response.data.items
        this.paging.total = response.data.total
        this.listLoading = false
      })
    }
  }
}
</script>


<style lang="scss" scoped>
.pagination{
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
